﻿namespace MsForgeReboot.Framework.ModelBinders
{
    using System.Web.Mvc;

    using MsForgeReboot.Framework.ViewModels;

    public class ImageViewModelModelBinder: IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
            var result = new ImageViewModel {
                                                Name = controllerContext.HttpContext.Request.Form["Name"],
                                                PostedFile = controllerContext.HttpContext.Request.Files[0]
                                            };

            if (string.IsNullOrEmpty(result.Name)) {
                result.Name = result.PostedFile.FileName;
            }

            return result;
        }
    }
}
